(function(){
    angular.module('app').filter('toTrusted', ['$sce', function($sce) {
        return function(text) {
            return $sce.trustAsHtml(text);
        };
    }]).controller("agCreativeCtrl",function($scope,proxy,$stateParams){

        $scope.stateList = [
            {name: "全部审核状态"},
            {id: "1",name: "待审核"},
            {id: "2",name: "审核通过"},
            {id: "3",name: "审核拒绝"}
        ]
        $scope.filter = {
            advertiserID: $stateParams.id
        }
        $scope.dataType = [0, "	品牌名称/贊助信息", "描述信息", "用户评级", "点赞数", "下载数量",
            "价格", "售卖价格", "服务电话", "地址", "描述信息2", "URL", "评论数", "H5"];
        $scope.adTypes = [0, "banner", "开屏", "插屏", "视频贴片", "原生", "激励视频"];
        $scope.creativeTypes = [0, "单图", "一图三文", "文字链", "视频", "H5", "原生"];
        $scope.interactionTypes = [0, "仅展示", "下载应用", 0 ,"跳转网页"];
        $scope.selectState = function (item) {
            $scope.filter.auditState = item.id;
            getList()
        }
        $scope.search = function (value) {
            $scope.filter.name = value;
            getList();
        }
        $scope.isSingleImg = function(item) {
            var m = item.material || [];
            return (m.length == 1 && m[0].image);
        }

        $scope.showImg = function(url) {
            layer.photos({
                photos: {
                    "data": [
                      {
                        "src": url,
                      }
                    ]
                  }
                ,anim: 5
              });
        }

        $scope.showIframe = function(dom) {
            $scope.srcdoc = dom;
            layer.open({
                type: 1,
                shade: 0.3,
                closeBtn: 0,
                shadeClose: true,
                title: false, //不显示标题
                content: $('.iframe-box')
            })
        }
        $('.video-box video')[0].oncanplay = function () {
            this.play();
        }
        $scope.$on('ngFinishRender', function(){
            // 设置所有video的时间，防止部分浏览器在未播放时为黑屏
            $(".table-box video").each(function(){
                this.oncanplay = function(){
                    this.oncanplay = null;
                    this.currentTime = 4;
                    // this.pause();
                }
            })
        });
        $scope.showVideo = function(src) {
            var video = $('.video-box video')[0];
            if($scope.videoSrc == src) {
                video.currentTime = 0;
                video.pause();
            } else {
                $scope.videoSrc = src;
            }
            layer.open({
                type: 1,
                shade: 0.3,
                closeBtn: 0,
                shadeClose: true,
                area: ['500px', ''], //宽高
                title: false, //不显示标题
                content: $('.video-box'),
                cancel: function(){
                    video.pause()
                }
            })
        }
        getList();

        function getList(current, pagesize) {
            var data = {
                'page': (current || 1)+"",
                'size': (pagesize || 20)+"",
                "sort": ["cTime,1"],
                'filter': $scope.filter
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/creative/list',
                data: data,
                success: function(res){
                    // 获得列表
                    $scope.list = res.data.data;

                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        'hash': false,
                        callback:getList
                    });
                    var ids = [];
                    $scope.list.forEach(function (v) {
                        getPreview(v);
                        ids.push(v.id)
                    })
                    getAdxAudit(ids);

                }
            })
        }
        function getPreview (item) {
            var m = item.material || [];
            for(var i = 0; i < m.length; i++) {
                var v = m[i];
                if(v.image) {
                    item.hasImg = true;
                    item.preview = v.image.url;
                    break;
                }
                if(v.video) {
                    item.hasVideo = true;
                    item.preview = v.video.url;
                    break;
                }
                if(v.data && v.data.type == "13") {
                    item.hasHtml = true;
                    item.preview = v.data.content;
                    break;
                }
            }
        }

        function getAdxAudit(ids){
            if(ids.length == 0) return;
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/creative/adx/auditList',
                load: true,
                data: {
                    data: ids
                },
                success: function(res){
                    // 获得列表
                    ids.forEach(function(v,i){
                        $scope.list[i].adxAuditList = res.data[v];
                    })
                }
            })
        }
    })
})();